﻿/**
 * Time manager for games.
 * 
 * @file	SystemTimer.as
 * @author	Jorge Miranda
 * @version 1.1.0
*/

/*
 * History:
 * 
 * v 0.0.1 - First Release - Jorge Miranda
 * 		- Timer is Singleton
 * v 0.0.2 - Now you can pause de Timer with the pause() method. - Jorge Miranda
 * v 0.1.0 - New elapsed member.  Retrieves the number of milliseconds elapsed since the last timer's update. - Jorge Miranda
 * v 0.1.1 - Rename almost all the methods and variables names. - Jorge Miranda
 * v 1.0.0 - AS3 version - Jorge Miranda
 * v 1.1.0 - Renamed, made Static.
*/

package racingGame.core {
	
	public class Time 	
	{
		import flash.utils.getTimer;
		
		/*
	     * Variables
		*/
		
	    protected static	var _starttime			:Number=0;
	    protected static 	var _frames_per_second	:Number=0;
		protected static 	var _milliseconds		:Number=0;
		protected static	var _seconds			:Number=0;
		protected static 	var _minutes			:Number=0;
		protected static 	var _hours				:Number=0;
		protected static 	var _elapsed			:Number=0;
		
		
	
		/**
		 * Resets the timer. 
		*/
		public static function reset() :void
		{
			_starttime = getTimer ();
	        _milliseconds = 0;
		}
		
		
		/**
		 * Updates the timer.
		*/
	    public static function update () :void
	    {
				_elapsed = getTimer () - _starttime;
				_milliseconds += _elapsed;
				
				_frames_per_second = Math.floor (1000 / _elapsed);
				
				_seconds = Math.floor (_milliseconds / 1000);
				_minutes = Math.floor (_seconds/60);
				_hours = Math.floor (_minutes/60);
				
				_starttime = getTimer ();
			
	    }
	    
		
		/**
		 * Retrieves the number of _milliseconds _elapsed since the creation of the
		 * timer.
		*/
		public static function get milliseconds () :Number
		{
			return _milliseconds;
		}
		
		
		/**
		 * Retrieves the number of _seconds _elapsed since the creation of the
		 * timer.
		*/
		public static function get seconds () :Number
		{
			return _seconds;
		}
		
		
		/**
		 * Retrieves the number of _minutes _elapsed since the creation of the
		 * timer.
		*/
		public static function get minutes () :Number
		{
			return _minutes;
		}
		
		/**
		 * Retrieves the number of _milliseconds _elapsed since the last timer's update.
		*/
		public static function get elapsed () :Number
		{
			return _elapsed;
			
		}
		
		
		/**
		 * Retrieves the number of _hours _elapsed since the creation of the
		 * timer.
		*/
		public static function get hours  () :Number {
			return _hours;
		}
		
		
		/**
		 * Retrieves the effective FPS meassured.
		*/
		public static function get FPS () :Number {
			return _frames_per_second;
		}
		
		
		/**
		 * Formats a specified number of milliseconds
		 * @param	milliseconds Milliseconds
		 * @param	format Units of time, separated by ":". 
		 * Posible units of time' strings are 'hh', 'h', 'mm', 'm', 'ss', 's', 'd', 'cc', 'c';
		 * @return
		 */
		 
		public static function format (milliseconds:Number, format:String = "mm:ss") :String {
			
			// Seconds, Centisecond, Decisecond , Minutes and Hours
			var seg	:Number = Math.floor(milliseconds / 1000);			
			var cs	:Number = Math.floor(milliseconds/10) - 100 * seg;				
			var ds	:Number = Math.floor(milliseconds/100) - 10 * seg;				
			var min	:Number = Math.floor(seg / 60);			
			var hr	:Number = Math.floor(min / 60);	
			
			seg = seg - min * 60;
			
			// Format 
			var units :Array = format.split(":");
			var formatedTime :String = "";
			
			for (var i:uint = 0; i < units.length; i++)
			{
				var addZero	:Boolean = false;
				var fs		:String = units[i];
				var ns		:String = "";
				
				switch(fs) {
					// Hours
					case "h":	ns = String(hr); break;
					case "hh":	ns = String(hr); addZero = true; break;
					// Minutes
					case "m":	ns = String(min); break;
					case "mm":	ns = String(min); addZero = true; break;
					// Seconds
					case "s":	ns = String(seg); break;
					case "ss":	ns = String(seg); addZero = true; break;
					// Decisecond
					case "d":	ns = String(ds); break;
					// Centisecond
					case "c":	ns = String(cs); break;
					case "cc":	ns = String(cs); addZero = true; break;
					
					default: "ERROR";
				}
				
				if (addZero && ns.length < 2) {
					ns = "0" + ns;
				}
				
				formatedTime += ":" + ns;
				
			}
			
			formatedTime = formatedTime.substr(1);
			
			return formatedTime;
		}	 
	}
}
